﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Net;
using System.Linq.Expressions;
using System.Reflection;
using System.Dynamic;


namespace Ys.Base.Common
{
    /// <summary>
    /// 常用返回请求返数据结果累实体
    /// </summary>
    public class ReturnResult<T> 
    {
        /// <summary>
        /// 执行开始时间
        /// </summary>
        protected DateTime _runStartTime = DateTime.Now;
        /// <summary>
        /// 执行结束时间
        /// </summary>
        protected DateTime _runEndTime;

        /// <summary>
        /// 执行时间
        /// </summary>
        private double _runTime = 0;

       

        /// <summary>
        /// 状态码
        /// </summary>
        public int Code { get; set; } = (int)HttpStatusCode.BadRequest;
        /// <summary>
        /// 数据状态
        /// </summary>
        public bool Status { get; set; } = false;
        /// <summary>
        /// 消息
        /// </summary>
        public string Message { get; set; } = "Not";


        /// <summary>
        /// 返回时间
        /// </summary>
        public long GetTime { get; set; } = DateTime.Now.ToTimeStampSeconds();

        /// <summary>
        /// 执行时间
        /// </summary>
        public double RunTime {
            get {
                RunEndTime();
                return _runTime;
            }
            set { _runTime = value; }
        }

        /// <summary>
        /// 执行计时开始
        /// </summary>
        public void RunStartTime()
        {
            _runStartTime = DateTime.Now;
        }

        /// <summary>
        /// 计时结束
        /// </summary>
        public void RunEndTime()
        {
            _runEndTime = DateTime.Now;
            var time = _runEndTime - _runStartTime;

            _runTime = _runTime == 0 ? time.TotalMilliseconds : _runTime;

        }
      
        

        /// <summary>
        /// 数据data
        /// </summary>
        public T Data {
            get;set;
        }

        /// <summary>
        /// 其他数据 可以看做为第二个Data读取或使用
        /// </summary>
        public object RemarkData { get; set; }

        /// <summary>
        /// 
        /// </summary>
        public ReturnResult()
        {
            RunStartTime();
        }
        /// <summary>
        /// 全数据创建 当Data为null时 status自动为false core为400
        /// </summary>
        /// <param name="data">绑定的数据</param>
        /// <param name="status">状态默认 true</param>
        /// <param name="core">状态码默认200</param>
        /// <param name="message">消息默认OK</param>
        public ReturnResult(T data, string message = "ok", bool status = true, int core = (int)HttpStatusCode.OK)
        {

            Data = data;
            Status = status;
            Code = core; 
            Message = message;
            if (data == null && status == true)
            {
                status = false;
            }
            RunStartTime();
        }
        /// <summary>
        /// 设计设置ok并返回
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="Data">Data数据</param>
        /// <param name="code">状态编号</param>
        /// <returns></returns>
        public ReturnResult<T> OkResult( string msg = null, object Data = null,int code=200)
        {
            this.OK(msg,Data,code);
            return this;
        }
       

        /// <summary>
        /// 数据设置ok
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="Data">data数据</param>
        /// <param name="code">状态编码</param>
        public void OK(string msg = null,object Data=null,int code=200)
        {
            Status = true;
            Code = code;
          Message=  msg != null ? msg : "ok";
           
            if (Data != null)
            {
                try
                {
                    this.Data = (T)Data;
                }
                catch { }
            }

        }

        /// <summary>
        /// 数据设置not
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="code">code码</param>
        public void Not(string msg = null, int code = 400)
        {

            Status = false;
            Code = 400;
            if (code != 400)
            {
                Code = code;
            }
            if (msg != null)
            {
                Message = msg;
            }
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="msg">消息</param>
        /// <param name="code">Data数据</param>
        /// <returns></returns>
        public ReturnResult<T> NotResult(string msg = null, int code=400)
        {
            this.Not(msg,code);
            return this;
        }


    }


   
}
